//// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
//package com.linqingying.gitcode.pullrequest.ui.comment
//
//import com.intellij.collaboration.async.classAsCoroutineName
//import com.intellij.collaboration.async.computationState
//import com.intellij.collaboration.async.mapDataToModel
//import com.intellij.collaboration.async.transformConsecutiveSuccesses
//import com.intellij.collaboration.util.getOrNull
//import com.intellij.openapi.project.Project
//import com.intellij.platform.util.coroutines.childScope
//import kotlinx.coroutines.CoroutineScope
//import kotlinx.coroutines.flow.*
//import com.linqingying.gitcode.api.data.pullrequest.GCPullRequestReviewThread
//import com.linqingying.gitcode.pullrequest.data.GCPRDataContext
//import com.linqingying.gitcode.pullrequest.data.provider.GCPRDataProvider
//import com.linqingying.gitcode.pullrequest.data.provider.createThreadsRequestsFlow
//import com.linqingying.gitcode.pullrequest.ui.editor.GCPRReviewNewCommentEditorViewModel
//import com.linqingying.gitcode.pullrequest.ui.editor.GCPRReviewNewCommentEditorViewModelImpl
//
//internal class GCPRThreadsViewModels(
//  private val project: Project,
//  parentCs: CoroutineScope,
//  private val dataContext: GCPRDataContext,
//  private val dataProvider: GCPRDataProvider,
//) {
//  private val cs = parentCs.childScope(classAsCoroutineName())
//  val canComment: Boolean = dataProvider.reviewData.canComment()
//
//  val compactThreads: StateFlow<Collection<GCPRCompactReviewThreadViewModel>> =
//    dataProvider.reviewData.createThreadsRequestsFlow()
//      .computationState()
//      .transformConsecutiveSuccesses(false) {
//        mapDataToModel(GCPullRequestReviewThread::id,
//                       { createThread(it) },
//                       { update(it) })
//      }.map { it.getOrNull().orEmpty() }
//      .stateIn(cs, SharingStarted.Lazily, emptyList())
//
//  private fun CoroutineScope.createThread(initialData: GCPullRequestReviewThread) =
//    UpdateableGCPRCompactReviewThreadViewModel(project, this, dataContext, dataProvider, initialData)
//
//  private val _newComments = MutableStateFlow<Map<GCPRReviewCommentPosition, GCPRReviewNewCommentEditorViewModelImpl>>(emptyMap())
//  val newComments: StateFlow<Map<GCPRReviewCommentPosition, GCPRReviewNewCommentEditorViewModel>> = _newComments.asStateFlow()
//
//  fun requestNewComment(location: GCPRReviewCommentPosition): GCPRReviewNewCommentEditorViewModel =
//    _newComments.updateAndGet { currentNewComments ->
//      if (!currentNewComments.containsKey(location)) {
//        val vm = createNewCommentVm(location)
//        currentNewComments + (location to vm)
//      }
//      else {
//        currentNewComments
//      }
//    }[location]!!
//
//  fun cancelNewComment(location: GCPRReviewCommentPosition) =
//    _newComments.update {
//      val oldVm = it[location]
//      val newMap = it - location
//      oldVm?.destroy()
//      newMap
//    }
//
//  private fun createNewCommentVm(position: GCPRReviewCommentPosition) =
//    GCPRReviewNewCommentEditorViewModelImpl(project, cs, dataProvider,
//                                            dataContext.repositoryDataService.remoteCoordinates.repository,
//                                            dataContext.securityService.currentUser,
//                                            dataContext.avatarIconsProvider,
//                                            position) {
//      cancelNewComment(position)
//    }
//}
